// 
// Decompiled by Procyon v0.5.36
// 

package com.orange.boot.dev1.controller;

import org.slf4j.LoggerFactory;
import java.util.List;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import com.orange.boot.dev1.entity.User;
import org.springframework.web.bind.annotation.PostMapping;
import io.swagger.annotations.ApiOperation;
import com.orange.boot.utils.R;
import org.springframework.web.bind.annotation.RequestBody;
import com.orange.boot.dev1.entity.UserVo;
import org.springframework.web.client.RestTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import com.orange.boot.dev1.service.UserService;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;

@RestController("webUserController")
@CrossOrigin
@RequestMapping({"/web/user"})
@Api(
        tags = {"web用户接口"}
)
@Component
public class UserController {
    private static final Logger log = LoggerFactory.getLogger(UserController.class);
    @Autowired
    UserService userService;
    private final String uri = "http://localhost:8806";
    @Autowired
    RestTemplate restTemplate;

    public UserController() {
    }

    @ApiOperation("登录")
    @PostMapping({"login"})
    public R login(@RequestBody UserVo userVo) {
        return this.userService.login(userVo);
    }

    @ApiOperation("添加")
    @PostMapping({"add"})
    public R addUser(@RequestBody User user) {
        return this.userService.addUser(user);
    }

    @ApiOperation("修改")
    @PostMapping({"update"})
    public R update(@RequestBody User user) {
        return this.userService.updateUser(user);
    }

    @ApiOperation("根据状态查询")
    @PostMapping({"selectByStatus"})
    public R selectByStatus(Integer status) {
        this.userService.listByStatus(status);
        return R.ok();
    }

    @ApiOperation("查询")
    @GetMapping({"/selectById/{id}"})
    public R select(@PathVariable("id") String id) {
        User user = (User)this.restTemplate.getForObject("http://localhost:8806/getUserById/" + id, User.class, new Object[0]);
        if (user != null) {
            user.setPassword("");
        }

        return R.ok().data("user", user);
    }

    @ApiOperation("删除")
    @PostMapping({"/delete/{id}"})
    public R delete(@PathVariable("id") String id) {
        this.restTemplate.delete("http://localhost:8806/deleteUser/" + id, new Object[0]);
        return R.ok();
    }

    @ApiOperation("获取所有用户")
    @PostMapping({"/getAllUser"})
    public R getUserList() {
        String userObjects = (String)this.restTemplate.getForObject("http://localhost:8806/getUserList", String.class, new Object[0]);
        List<User> users = JSONObject.parseArray(userObjects, User.class);
        return R.ok().data("user", users);
    }

    @ApiOperation("注册")
    @PostMapping({"register"})
    public R register(@RequestBody User user) {
        return this.userService.register(user);
    }

    @ApiOperation("赋权")
    @PostMapping({"/power/{id}/{powerId}"})
    public R register(@PathVariable("id") String id, @PathVariable("powerId") Integer powerId) {
        Boolean flag = (Boolean)this.restTemplate.postForObject("http://localhost:8806/getPower/" + id + "/" + powerId, (Object)null, Boolean.class, new Object[0]);
        return R.ok().success(flag);
    }

    //下面两个接口是东炜负责的部分，暂时用不上，该接口可不使用
    @ApiOperation("详细信息")
    @PostMapping ("detailInfo")
    public R detailInfo(@RequestBody UserVo userVo){
        return userService.detailInfo(userVo);
    }
    @ApiOperation("修改信息")
    @PostMapping("reviseInfo")
    public R detailInfo(@RequestBody User user){
        return userService.reviseInfo(user);
    }
}

